/*
 * To change this license header, choose License Headers in Project Properties.
 * To change this template file, choose Tools | Templates
 * and open the template in the editor.
 */

package com.googlecode.jsimplemarket.wicket.pages;

import com.googlecode.jsimplemarket.model.Creanta;
import com.googlecode.jsimplemarket.model.Firma;
import com.googlecode.jsimplemarket.model.Inchiriere;
import com.googlecode.jsimplemarket.model.Absenta;
import com.googlecode.jsimplemarket.model.Plata;
import com.googlecode.jsimplemarket.model.SpatiuComercial;
import com.googlecode.jsimplemarket.model.Taxa;
import com.googlecode.jsimplemarket.repository.GenericOrder;
import com.googlecode.jsimplemarket.repository.GenericProperty;
import com.googlecode.jsimplemarket.service.GenericService;
import com.googlecode.jsimplemarket.service.InchiriereService;
import com.googlecode.jsimplemarket.service.AbsentaService;

import java.math.BigDecimal;
import java.text.DateFormat;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import java.util.Locale;

import org.apache.wicket.behavior.AttributeAppender;
import org.apache.wicket.markup.html.WebMarkupContainer;
import org.apache.wicket.markup.html.basic.Label;
import org.apache.wicket.markup.html.form.ChoiceRenderer;
import org.apache.wicket.markup.html.form.DropDownChoice;
import org.apache.wicket.markup.html.form.Form;
import org.apache.wicket.markup.html.form.SubmitLink;
import org.apache.wicket.markup.html.link.Link;
import org.apache.wicket.markup.html.list.ListItem;
import org.apache.wicket.markup.html.list.ListView;
import org.apache.wicket.model.CompoundPropertyModel;
import org.apache.wicket.model.LoadableDetachableModel;
import org.apache.wicket.model.Model;
import org.apache.wicket.model.PropertyModel;
import org.apache.wicket.spring.injection.annot.SpringBean;

/**
 *
 * @author Alexandru.Codescu
 */
public class IstoricAbsentePage extends DefaultViewPage {
    @SpringBean(name = "genericService")
    private GenericService<SpatiuComercial> spatiuComercialService;
    @SpringBean(name = "genericService")
    private GenericService<Firma> firmaService;
    @SpringBean(name = "genericService")
    private GenericService<Inchiriere> inchiriereService;
    @SpringBean(name = "inchiriereService")
    private InchiriereService serviceInchiriere;
    @SpringBean(name = "genericService")
    private GenericService<Creanta> serviceCreanta;
    @SpringBean(name = "genericService")
    private GenericService<Plata> servicePlata;
    @SpringBean(name = "genericService")
    private GenericService<Absenta> serviceAbsenta;
    @SpringBean(name = "absentaService")
    private AbsentaService absentaService;
    
    private Locale locale = new Locale("ro","RO");
    public String printStandardDate(Date data) {
        return new SimpleDateFormat("dd.MM.yyyy HH:mm").format(data);
    }
    
    public IstoricAbsentePage(final Long idSpatiuComercial) {
        SpatiuComercial spatiuComercial = spatiuComercialService.getById(SpatiuComercial.class, idSpatiuComercial);
        final Label lblTaraba = new Label("lblTaraba", spatiuComercial.getCod());
        add(lblTaraba);
        add(new AbsenteView("istoric", absentaService.queryByTaraba(idSpatiuComercial)));
    }
    
    private class AbsenteView extends ListView<Object[]> {

        public AbsenteView(String id, List<Object[]> data) {
            super(id, data);
        }

        @Override
        protected void populateItem(ListItem<Object[]> li) {
            final Object[] object = li.getModelObject();
            int index;
            if (object==null){
            	index = 0;
            } else {
            	index = li.getIndex() + 1;
            }
            
            li.add(new Label("index", object==null ? "-" : index));
            //li.add(new Label("taraba", object==null ? "-" : object[0].toString()));
            
            /* Link lnkTaraba = new Link("lnkTaraba") {
                @Override
                public void onClick() {
                    setResponsePage(new TarabaViewPage((Long) object[2]));
                }
            };
            lnkTaraba.add(new Label("taraba", object==null ? "-" : object[0].toString()));
            li.add(lnkTaraba); */
            
            li.add(new Label("ocupant", object==null ? "-" : object[1].toString()));            
            // li.add(new Label("dataAbsenta", object==null ? "-" : DateFormat.getTimeInstance(DateFormat.SHORT, locale).format(object[4])));
            li.add(new Label("dataAbsenta", object==null ? "-" : (new SimpleDateFormat("dd.MM.YYYY").format(object[3]))));
            
            Link delLink = new Link("delLink") {
                @Override
                public void onClick() {

                    Absenta absenta = serviceAbsenta.getById(Absenta.class, Long.valueOf(object[4].toString()));
                    serviceAbsenta.delete(absenta);

                    setResponsePage(new IstoricAbsentePage((Long) object[2]));
                }
            };
            li.add(delLink);
            
//            if (object[0]!=null){
//                Link delLink = new Link("delLink") {
//                    @Override
//                    public void onClick() {
//
//                        Absenta absenta = serviceAbsenta.getById(Absenta.class, Long.valueOf(object[4].toString()));
//                        serviceAbsenta.delete(absenta);
//
//                        //setResponsePage(new RaportAbsentePage());
//                    }
//                };
//                li.add(delLink);
//            } else {
//                Link delLink = new Link("delLink") {
//                    @Override
//                    public void onClick() {
//                        setResponsePage(new RaportAbsentePage());
//                    }
//                };
//                li.add(delLink);
//            }
            
        }
    }
}
